package com.tz.fruit.controller;

import com.tz.fruit.entity.Address;
import com.tz.fruit.entity.User;
import com.tz.fruit.service.IAddressService;
import com.tz.fruit.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by asus on 2017/7/19.
 */
@Controller
@RequestMapping("/addr")
public class AddressAction {

    @Autowired
    private IAddressService addressService;

    @Autowired
    private IUserService userService;

    @RequestMapping("/list")
    public String addressView(Model model,HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Address> addrs = addressService.findAll(user.getUserId());
        model.addAttribute("address", addrs);
        return "addr/address";
    }

    /**
     * 负责添加或修改新地址
     *
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(HttpSession session, Address address) {
        User user = (User) session.getAttribute("user");
        List<Address> addrs = addressService.findAll(user.getUserId());
        if ("on".equals(address.getIsDefault())) {
                for (Address a : addrs) {
                    a.setIsDefault("0");
                    addressService.update(a);
                }
            address.setIsDefault("1");
        }/*else {
            address.setIsDefault("0");
        }*/

        if (address.getAddressId() == null) {
            address.setUser(user);
            addressService.save(address);
        } else {
            address.setUser(user);
            addressService.update(address);
        }
        return "redirect:list";
    }

    /**
     * 负责删除地址
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Long id) {
        addressService.deleteById(id);
        return "redirect:list";
    }

    /**
     * 负责获取需要修改的地址
     */
    @RequestMapping("/updatePre")
    public String updatePre(Model model, Long id,HttpSession session) {
        User user = (User) session.getAttribute("user");
        Address address = addressService.findById(id);
        model.addAttribute("addr", address);
        List<Address> addrs = addressService.findAll(user.getUserId());
        model.addAttribute("address", addrs);
        return "addr/address";
    }

    /**
     * 负责修改是否是默认地址
     */
    @RequestMapping("/changeIsDefault")
    public String changeIsDefault(Long addressId,HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Address> addrs = addressService.findAll(user.getUserId());
        Address address = addressService.findById(addressId);
        System.out.println(address.getIsDefault());
            for (Address a : addrs) {
                a.setIsDefault("0");
                addressService.update(a);
            }
            address.setIsDefault("1");
            addressService.update(address);
        System.out.println(address.getIsDefault());
        return "redirect:list";
    }

}
